package com.mock.ems.controller.common;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.mock.ems.utils.UploadUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MOCK on 2017/8/6.
 */
public class UploadController extends Controller {

    public void fupload() {

        List<UploadFile> uploadFileList = getFiles();
        UploadFile uploadFile = uploadFileList.get(0);
        Map<String,Object> jsonObject = new HashMap<>();
        String qnURL = UploadUtil.upload(uploadFile.getFile());
        if (null == qnURL) {
            qnURL = "http://www.zmzcloud.com/ems/upload/" + uploadFile.getFileName();
        }
        HttpServletResponse response = getResponse();
        response.setContentType("text/plain;charset=UTF-8");
        jsonObject.put("error", 0);
        jsonObject.put("url",qnURL);
        String json = "";
        PrintWriter pw = null;
        try {
            json = JSON.toJSONString(jsonObject);
            pw = response.getWriter();
            pw.write(json);
            pw.flush();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            pw.close();
        }
        renderNull();
    }
}
